package com.gs.djys.signtext;


import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaScannerConnection;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.gs.djys.Base.Urls;
import com.gs.djys.R;
import com.gs.djys.Util.ToastUtils;
import com.gs.djys.Util.Utils;
import com.gs.djys.callback.StringDialogCallback;
import com.gs.djys.mvp.MVPBaseActivity;
import com.kyanogen.signatureview.SignatureView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class SignTextActivity extends MVPBaseActivity<SignTextContract.View, SignTextPresenter> implements SignTextContract.View {


    @Bind(R.id.signature_view)
    SignatureView signatureView;
    @Bind(R.id.sign_te)
    TextView signTe;
    @Bind(R.id.qx_te)
    TextView qxTe;
    @Bind(R.id.back)
    TextView back;

    @Override
    public void onRequestError(String msg) {
        ToastUtils.showShortToast(msg);
    }

    @Override
    protected int getLayout() {
        return R.layout.act_sign_te;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        int colorPrimary = ContextCompat.getColor(this, R.color.text_333);
        signatureView.setPenColor(colorPrimary);
        signTe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File directory = Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                File file = new File(directory, System.currentTimeMillis() + ".png");
                FileOutputStream out = null;
                Bitmap bitmap = signatureView.getSignatureBitmap();
                try {
                    out = new FileOutputStream(file);
                    if (bitmap != null) {
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                        Intent intent = new Intent();
                        intent.putExtra("path",file.getPath());
                        intent.putExtra("nameStr",getIntent().getStringExtra("nameStr"));
                        intent.putExtra("strBlob",Utils.bmpToBase64String(file.getPath()));
                        setResult(-2, intent);
                        finish();
                    } else {
                        throw new FileNotFoundException();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        qxTe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                signatureView.clearCanvas();
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
